<?php

if (!defined('PUBLIC_HTML'))
{
	exit;
}

$submit = (isset($_POST['submit'])) ? true : false;

if ($submit)
{
	$media_file = $file->upload('file', 'media/content/');
	
	// Get the file extension
	$file_extension = $file->get_extension($media_file['name']);
	
	// Rename the file to get a unique filename
	$new_filename = $file->rename('media/content/', $media_file['name'], md5(date('Y-m-d H:i:s') . $media_file['name']) . '.' . $file_extension);
	
	// Get the width and height, if it is an image
	if ($_POST['type'] == MEDIA_TYPE_IMAGE)
	{
		$img = $image->get_size('media/content/' . $new_filename);
	}
	else
	{
		$img['width'] = '0';
		$img['height'] = '0';
	}
	
	$post = array();
	$post['group_id']		= 0;
	$post['type']			= $_POST['type'];
	$post['language']		= $language->current();
	$post['title']			= $_POST['title'];
	$post['description']	= $_POST['description'];
	$post['datetime']		= date('Y-m-d H:i:s');
	$post['file']			= $new_filename;
	$post['extension']		= $file_extension;//$media_file['extension'];
	$post['size']			= $media_file['size'];
	$post['width']			= $img['width'];//$media_file['width'];
	$post['height']			= $img['height'];//$media_file['height'];
	try
	{
		$add_file = $database->sqlInsert(TABLE_MEDIA, $post, 'med_');
	}
	catch (PDOException $e)
	{
		$error->pdo_error('An error occured while adding the data to the database', $e->getMessage(), $e->getFile(), $e->getLine());
	}
	
	unset($post);
	$post = array();
	$post['med_group_id']	= $add_file;
	try
	{
		$database->sqlUpdate(TABLE_MEDIA, $post, 'med_id', $add_file);
	}
	catch (PDOException $e)
	{
		$error->pdo_error('An error occured while adjusting the group value', $e->getMessage(), $e->getFile(), $e->getLine());
	}
	
	redirect('cms.php?p=media');
	exit;
}

?>